BEGIN { push(@INC,'./build/perl');}
use Scopes;
use Scopes::Net;
use Scopes::Functions;

use Devel::StackTrace;
sub test{
   if ($_[0]){
      print $_[1]."... Ok\n";
   } else {
      die "Error during ".$_[1]."\n";
   }
}

$SIG{__DIE__} = sub {
        my $error = shift;
        my $trace = Devel::StackTrace->new;
        print "==========================================================================\nError: $error\n",
              "Stack Trace:\n",
              $trace->as_string;
};

$|=1;

# 0 + 1 (c 10)=> 2 + 3
# 4 isa 1 channel 0
# 5 isa 1 channel 1
# 6 isa 2 channel 0
# 7 isa 2 channel 1
# 7 (i 15)=> 8
# 8|- => 9p
# 9|- (c 11)=> 12
# 8 => 13
# 9 => 14

$n=new Scopes::Net;
$n->addRea({c0=>1,c1=>1},{c2=>1,c3=>1},1,[c10]);
$n->setMembers(c1,[c4,c5]);
$n->setChannels(c4,[UP1]);
$n->setChannels(c5,[UP2]);
$n->setMembers(c2,[c6,c7]);
$n->setChannels(c6,[UP1]);
$n->setChannels(c7,[UP2]);
$n->setChannels(c1,[UP1,UP2]);
$n->setChannels(c2,[UP1,UP2]);
$n->addRea({c7=>1},{c8=>1},1,undef,[c15]);
$n->addRea({c8=>1},{c13=>1},1);
$n->addRea({c9=>1},{c14=>1},1);
$n->addRea({c8=>1},{c9=>1},1,undef,undef,[c8],[c9]);
$n->addRea({c9=>1},{c12=>1},1,[c11],undef,[c9]);
$n->print_matrix;

$chn=initChannels($n);
$states=new Scopes::StateList;
dScopeRun($n,0,1000,$states,$n->cIDMS([c0,c1,c10]),undef,$chn);
test(join('',sort @{$n->cMSID($states->last()->{present})}) eq 'c0c1c10c13c14c2c3c4c5c6c7c8c9',"family seed");
$chn->clear();
$states=dScope($n,1000,$n->cIDMS([c0,c1]),undef,$chn);
test(join('',sort @{$n->cMSID($states->last()->{present})}) eq 'c0c1c4c5',"no catalyst");
$chn->clear();
$states=dScope($n,1000,$n->cIDMS([c0,c4,c10]),undef,$chn);
test(join('',sort @{$n->cMSID($states->last()->{present})}) eq 'c0c1c10c2c3c4c6',"family members seed");
$chn->clear();
$states=dScope($n,1000,$n->cIDMS([c0,c1,c10,c15]),undef,$chn);
test(join('',sort @{$n->cMSID($states->last()->{present})}) eq 'c0c1c10c15c2c3c4c5c6c7',"reaction inhibition");
$chn->clear();
$states=dScope($n,1000,$n->cIDMS([c0,c1,c10,c11]),undef,$chn);
for (my $i=0;$i<$states->size();$i++){
   print join(' ', sort @{$n->cMSID($states->at($i)->combine())})."\n";
}
test(join('',sort @{$n->cMSID($states->last()->combine)}) eq 'c0c1c10c11c12c2c3c4c5c6c7',"substrate inhibition & propagation");


